package org.example.algorithm.dichotomy;

/**
 * 搜索插入位置
 */

public class SearchInsertSolution {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 6};
        int target = 2;
        int pos = searchInsert(nums, target);
        System.out.println(pos);
    }

    public static int searchInsert(int[] nums, int target) {
        int low = 0;
        int high = nums.length - 1;
        while (low <= high) {
            int middle = (low + high) / 2;
            if (nums[middle] > target) {
                high = middle - 1;
            } else if(nums[middle] < target) {
                low = middle + 1;
            } else {
                return middle;
            }
        }
        return low;
    }
}
